/**
 * fileName: SL_YLES_Import_item_barcode_History
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */

import {EntryPoints} from 'N/types'
import search = require('N/search')
import ui = require('N/ui/serverWidget')
import file = require('N/file')
import https = require('N/https')
import runtime = require('N/runtime')
import url = require('N/url')
import log = require('N/log')
import {createImportResultForm, createUI} from "../tools/YLES_UI_FORM";
import {CustomRecordType} from "../tools/YLES_RELATED_RECORDTYPE";
import {Form} from "N/ui/serverWidget";

let req: https.ServerRequest, res: https.ServerResponse, params: any;

export function onRequest(cxt: EntryPoints.Suitelet.onRequestContext) {
    req = cxt.request;
    res = cxt.response;
    params = req.parameters;
    if (req.method === 'GET') {
        if (params.logId > 0) {
            let irForm = ui.createForm({title: 'Import Result Detail'});
            irForm.clientScriptModulePath = './CS_YLES_Import_item_barcode_History.js'

            //创建导入详情界面
            createImportResultForm(params.logId,irForm);

            res.writePage(irForm);
        } else {
            let ihForm = createImportHistoryForm();
            res.writePage(ihForm);
        }
    }
}

//创建所有导入明细界面
function createImportHistoryForm() {
    const ihForm = ui.createForm({title: 'Import History'});
    const sublist = ihForm.addSublist({id: 'info_list', label: 'Details', type: ui.SublistType.STATICLIST});
    sublist.addField({id: 'line_file', label: 'File Name', type: ui.FieldType.TEXT});
    sublist.addField({id: 'line_state', label: 'Status', type: ui.FieldType.TEXT});
    sublist.addField({id: 'line_created', label: 'Created Time', type: ui.FieldType.TEXT});
    sublist.addField({id: 'line_completed_time', label: 'Completed Time', type: ui.FieldType.TEXT});
    sublist.addField({id: 'line_link', label: 'Detail', type: ui.FieldType.TEXT});

    const importHistory = getImportLogList();
    importHistory.map(function (ih, index) {
        const link = url.resolveScript({
            scriptId: 'customscript_yles_import_csv_history_sl',
            deploymentId: 'customdeploy_yles_import_csv_history_sl', params: {logId: ih.id}
        });
        const html = `<a href="${link}" target="_blank" class="dottedlink">View Detail</a>`;
        sublist.setSublistValue({id: 'line_file', value: ih.fileName, line: index});
        sublist.setSublistValue({id: 'line_state', value: ih.state, line: index});
        sublist.setSublistValue({id: 'line_created', value: ih.created, line: index});
        if (ih.completedTime) {
            sublist.setSublistValue({id: 'line_completed_time', value: ih.completedTime, line: index});
        }
        sublist.setSublistValue({id: 'line_link', value: html, line: index});
    });
    return ihForm;
}

//获取所有导入明细
function getImportLogList() {
    let cur_user = runtime.getCurrentUser().id;
    let Search = search.create({
        type: CustomRecordType().IMPORT_ITEM_BARCODE_LOG,
        filters: [
            ['custrecord_yles_status', 'anyof', [3, 6]], 'and',//导入中以及导入成功
            ['owner.internalid', 'anyof', cur_user]
        ],
        columns: [
            {name: 'custrecord_yles_csv_name'},
            {name: 'custrecord_yles_status'},
            {name: 'created', sort: search.Sort.DESC},
            {name: 'custrecord_yles_end_time'},
        ]
    });
    let logList: { id: string; fileName: string; state: string; created: string; completedTime: string }[] = [];
    Search.run().each(function (res) {
        logList.push({
            id: res.id,
            fileName: res.getValue(Search.columns[0]) as string,
            state: res.getText(Search.columns[1]),
            created: res.getValue(Search.columns[2]) as string,
            completedTime: res.getValue(Search.columns[3]) as string
        })
        return true;
    });
    return logList;
}